<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .w {
            width: 1200px;
            margin: 0 auto;
        }

        .hender {
            width: 100%;
            height: 200px;
            background-color: blue;
        }

        .banner {
            width: 100%;
            height: 300px;
            background-color: pink;
        }

        .nav {
            width: 100%;
            height: 500px;
            background-color: purple;
        }

        .footer {
            width: 100%;
            height: 800px;
            background-color: yellow;
        }

        .slider_bar {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            right: 110px;
            width: 50px;
            height: 100px;
            text-align: center;
            background-color: hotpink;
        }

        span {
            display: none;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="w">
        <div class="hender">
            头部
        </div>
        <div class="slider_bar">
            <span>顶部</span>
        </div>
        <div class="banner">
            banner部分
        </div>
        <div class="nav">
            主体部分
        </div>
        <div class="footer">
            底部
        </div>
    </div>
    <script>
        var slider_bar = document.querySelector('.slider_bar');
        var banner = document.querySelector('.banner');
        var nav = document.querySelector('.nav');
        var span = document.querySelector('span')
        // console.log(banner.offsetTop);
        //banner的高度
        var bannerTop = banner.offsetTop;
        //slider_bar固定定位的高度
        var slider_barTop = slider_bar.offsetTop - bannerTop;
        //nav的高度
        var navTop = nav.offsetTop;
        //页面滚动的事件
        document.addEventListener('scroll', function () {
            // console.log(11);
            // console.log(window.pageYOffset);
            if (parseInt(window.pageYOffset) >= bannerTop) {
                slider_bar.style.position = 'fixed';
                slider_bar.style.top = slider_barTop + 'px';
            } else {
                slider_bar.style.position = 'absolute';
                slider_bar.style.top = '50%';

            }
            if (parseInt(window.pageYOffset) >= navTop) {
                span.style.display = 'block';

            } else {
                span.style.display = 'none';

            }
        });
        span.addEventListener('click', function () {
            animate(window, 0);
        });
        //动画函数
        function animate(obj, target, callback) {
            clearInterval(obj.timer);
            var timer = setInterval(function () {
                var step = (target - window.pageYOffset) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if (window.pageYOffset == target) {
                    clearInterval(timer);
                    if (callback) {
                        callback();
                    }
                }
                // obj.style.left = window.pageYOffset + step + 'px';
                window.scroll(0, window.pageYOffset + step);
            }, 15)
        }
    </script>
</body>

</html>